{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "dfeea84e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "from pathlib import Path\n",
    "import subprocess\n",
    "import signal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ec310324",
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "import sys\n",
    "sys.path.insert(0, '/home/julien/Software/Others/OpenStudio/src/cli/')\n",
    "from test_measure_manager import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8e906687",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using port=8090\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'http://localhost:8090'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "port = get_open_port_for_serving()\n",
    "URL = f\"http://{HOST}:{port}\"\n",
    "URL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "63437bc8",
   "metadata": {},
   "outputs": [],
   "source": [
    "OPENSTUDIO_CLI = Path('/home/julien/Software/Others/OS-build/Products/openstudio')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "80a3567b",
   "metadata": {},
   "outputs": [],
   "source": [
    "p = subprocess.Popen([str(OPENSTUDIO_CLI), 'labs', 'measure', '-s', str(port)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "baffd1b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "p.poll()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "f9631ac9",
   "metadata": {},
   "outputs": [],
   "source": [
    "p.send_signal(signal.SIGINT)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "9291b304",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'my_measures_dir': '', 'status': 'running'}"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r = requests.get(f\"{URL}\")\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "39d952bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.get(f\"{URL}/internal_state\")\n",
    "r.raise_for_status()\n",
    "assert r.json() == {'my_measures_dir': '', 'osm': [], 'status': 'running'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "4b237446",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'my_measures_dir': '', 'osm': [], 'status': 'running'}"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "58983952",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_measures_dir = str(Path('~/OpenStudio/Measures').expanduser())\n",
    "r = requests.post(url=f\"{URL}/set\", json={\"my_measures_dir\": my_measures_dir})\n",
    "r.raise_for_status()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "d3dccb4e",
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.get(f\"{URL}/internal_state\")\n",
    "r.raise_for_status()\n",
    "assert r.json() == {'my_measures_dir': my_measures_dir, 'osm': [], 'status': 'running'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "2217a44f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'my_measures_dir': '/home/julien/OpenStudio/Measures',\n",
       " 'osm': [],\n",
       " 'status': 'running'}"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "e8534851",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import openstudio\n",
    "OSM_PATH = Path('/media/DataExt4/Software/Others/OS-build3/model.osm')\n",
    "OSM_PATH2 = Path('/media/DataExt4/Software/Others/OS-build3/model2.osm')\n",
    "\n",
    "model = openstudio.model.Model()\n",
    "model.save(str(OSM_PATH), True)\n",
    "model = openstudio.model.Model()\n",
    "model.save(str(OSM_PATH2), True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "d9bf1f1f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'9FA60624'"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "openstudio.checksum(openstudio.toPath(str(OSM_PATH)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "c5653e94",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[38;2;255;000;000m┌────────────────────────────────────────────────────────────────────────────────┐\n",
      "│         The `labs` command is experimental - Do not use in production          │\n",
      "└────────────────────────────────────────────────────────────────────────────────┘\u001b[0m\n",
      "\n",
      "MeasureUpdateOptions:\n",
      "directoryPath=\n",
      "update=false\n",
      "update_all=false\n",
      "compute_arguments_model=\n",
      "run_tests=false\n",
      "server_port=8090\n",
      "\n",
      "\n",
      "Received GET request: GET / HTTP/1.1\r\n",
      "Accept: */*\r\n",
      "Accept-Encoding: gzip, deflate, br\r\n",
      "Connection: keep-alive\r\n",
      "Host: localhost:8090\r\n",
      "User-Agent: python-requests/2.28.1\r\n",
      "\r\n",
      "uri=/\n",
      "Received GET request: GET /internal_state HTTP/1.1\r\n",
      "Accept: */*\r\n",
      "Accept-Encoding: gzip, deflate, br\r\n",
      "Connection: keep-alive\r\n",
      "Host: localhost:8090\r\n",
      "User-Agent: python-requests/2.28.1\r\n",
      "\r\n",
      "uri=/internal_state\n",
      "Received POST request: POST /set HTTP/1.1\r\n",
      "Accept: */*\r\n",
      "Accept-Encoding: gzip, deflate, br\r\n",
      "Connection: keep-alive\r\n",
      "Content-Length: 55\r\n",
      "Content-Type: application/json\r\n",
      "Host: localhost:8090\r\n",
      "User-Agent: python-requests/2.28.1\r\n",
      "\r\n",
      "Received GET request: GET /internal_state HTTP/1.1\r\n",
      "Accept: */*\r\n",
      "Accept-Encoding: gzip, deflate, br\r\n",
      "Connection: keep-alive\r\n",
      "Host: localhost:8090\r\n",
      "User-Agent: python-requests/2.28.1\r\n",
      "\r\n",
      "uri=/internal_state\n",
      "Received POST request: POST /get_model HTTP/1.1\r\n",
      "Accept: */*\r\n",
      "Accept-Encoding: gzip, deflate, br\r\n",
      "Connection: keep-alive\r\n",
      "Content-Length: 67\r\n",
      "Content-Type: application/json\r\n",
      "Host: localhost:8090\r\n",
      "User-Agent: python-requests/2.28.1\r\n",
      "\r\n",
      "{\"osm_path\": \"/media/DataExt4/Software/Others/OS-build3/model.osm\"}Attempting to load model '/media/DataExt4/Software/Others/OS-build3/model.osm'\n",
      "Successfully loaded model '/media/DataExt4/Software/Others/OS-build3/model.osm'\n",
      "[openstudio.model.YearDescription] <1> 'UseWeatherFile' is selected in YearDescription, but there are no weather file set for the model.\n",
      "[openstudio.model.YearDescription] <1> 'UseWeatherFile' is selected in YearDescription, but there are no weather file set for the model.\n",
      "[utilities.idf.WorkspaceObject] <0> Object of type 'Schedule:Constant' and named 'Always On Discrete', points to an object named OnOff from field 1, but that object cannot be located.\n",
      "[utilities.idf.WorkspaceObject] <0> Object of type 'Schedule:Constant' and named 'Always Off Discrete', points to an object named OnOff 1 from field 1, but that object cannot be located.\n",
      "[utilities.idf.WorkspaceObject] <0> Object of type 'Schedule:Constant' and named 'Always On Continuous', points to an object named Fractional from field 1, but that object cannot be located.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'OK, loaded model with checksum 9FA60624'"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r = requests.post(url=f\"{URL}/get_model\", json={\"osm_path\": str(OSM_PATH)})\n",
    "r.raise_for_status()\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "b5354546",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'my_measures_dir': '/home/julien/OpenStudio/Measures',\n",
       " 'osm': [{'checksum': '9FA60624',\n",
       "   'osm_path': '/media/DataExt4/Software/Others/OS-build3/model.osm'}],\n",
       " 'status': 'running'}"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r = requests.get(f\"{URL}/internal_state\")\n",
    "r.raise_for_status()\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "bbf98b63",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'OK'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r = requests.post(url=f\"{URL}/get_model\", json={\"osm_path\": str(OSM_PATH)})\n",
    "r.raise_for_status()\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "d47442f7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "E59D919B\n",
      "Received POST request: POST /get_model HTTP/1.1\r\n",
      "Accept: */*\r\n",
      "Accept-Encoding: gzip, deflate, br\r\n",
      "Connection: keep-alive\r\n",
      "Content-Length: 67\r\n",
      "Content-Type: application/json\r\n",
      "Host: localhost:8090\r\n",
      "User-Agent: python-requests/2.28.1\r\n",
      "\r\n",
      "Using cached model /media/DataExt4/Software/Others/OS-build3/model.osm\n",
      "E59D919B\n",
      "Received POST request: POST /get_model HTTP/1.1\r\n",
      "Accept: */*\r\n",
      "Accept-Encoding: gzip, deflate, br\r\n",
      "Connection: keep-alive\r\n",
      "Content-Length: 67\r\n",
      "Content-Type: application/json\r\n",
      "Host: localhost:8090\r\n",
      "User-Agent: python-requests/2.28.1\r\n",
      "\r\n",
      "{\"osm_path\": \"/media/DataExt4/Software/Others/OS-build3/model.osm\"}Using cached model /media/DataExt4/Software/Others/OS-build3/model.osm\n",
      "E59D919B\n",
      "Received GET request: GET /internal_state HTTP/1.1\r\n",
      "Accept: */*\r\n",
      "Accept-Encoding: gzip, deflate, br\r\n",
      "Connection: keep-alive\r\n",
      "Host: localhost:8090\r\n",
      "User-Agent: python-requests/2.28.1\r\n",
      "\r\n",
      "uri=/internal_state\n",
      "Received POST request: POST /get_model HTTP/1.1\r\n",
      "Accept: */*\r\n",
      "Accept-Encoding: gzip, deflate, br\r\n",
      "Connection: keep-alive\r\n",
      "Content-Length: 68\r\n",
      "Content-Type: application/json\r\n",
      "Host: localhost:8090\r\n",
      "User-Agent: python-requests/2.28.1\r\n",
      "\r\n",
      "Attempting to load model '/media/DataExt4/Software/Others/OS-build3/model2.osm'\n",
      "Successfully loaded model '/media/DataExt4/Software/Others/OS-build3/model2.osm'\n",
      "[openstudio.model.YearDescription] <1> 'UseWeatherFile' is selected in YearDescription, but there are no weather file set for the model.\n",
      "[openstudio.model.YearDescription] <1> 'UseWeatherFile' is selected in YearDescription, but there are no weather file set for the model.\n",
      "[utilities.idf.WorkspaceObject] <0> Object of type 'Schedule:Constant' and named 'Always On Discrete', points to an object named OnOff from field 1, but that object cannot be located.\n",
      "[utilities.idf.WorkspaceObject] <0> Object of type 'Schedule:Constant' and named 'Always Off Discrete', points to an object named OnOff 1 from field 1, but that object cannot be located.\n",
      "[utilities.idf.WorkspaceObject] <0> Object of type 'Schedule:Constant' and named 'Always On Continuous', points to an object named Fractional from field 1, but that object cannot be located.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'OK, loaded model with checksum 608AB8B6'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r = requests.post(url=f\"{URL}/get_model\", json={\"osm_path\": str(OSM_PATH2)})\n",
    "r.raise_for_status()\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc3b9208",
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(url=f\"{URL}/download_bcl_measure\", json={\"\": \"\"})\n",
    "assert r.status_code == 400\n",
    "assert r.json() == 'Missing the uid in the post data'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "56727834",
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(url=f\"{URL}/download_bcl_measure\", json={\"uid\": \"baduuid\"})\n",
    "assert r.status_code == 400\n",
    "assert r.json() == \"Cannot find measure with uid='baduuid'\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f2d783f",
   "metadata": {},
   "outputs": [],
   "source": [
    "BCL_DIR = str(Path('~/BCL').expanduser())\n",
    "TDB_UID = '8890787b-8c25-4dc8-8641-b6be1b6c2357'\n",
    "OPENSTUDIO_RESULTS_UID = 'a25386cd-60e4-46bc-8b11-c755f379d916'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "546b89b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(url=f\"{URL}/download_bcl_measure\", json={\"uid\": TDB_UID})\n",
    "r.raise_for_status()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f92c1f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(url=f\"{URL}/bcl_measures\")\n",
    "r.raise_for_status()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f3fc224",
   "metadata": {},
   "outputs": [],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb1e6e5b",
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(url=f\"{URL}/reset\")\n",
    "r.raise_for_status()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6d12768b",
   "metadata": {},
   "outputs": [],
   "source": [
    "for endpoint in ['update_measures', 'compute_arguments', 'create_measure', 'duplicate_measure']:\n",
    "    r = requests.post(url=f\"http://{URL}/{endpoint}\")\n",
    "    assert r.status_code == 501"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cad18dd7",
   "metadata": {},
   "outputs": [],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "203ca62b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
